package cn.tedu.egarticle.base.filters;


import cn.tedu.egarticle.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Slf4j
@WebFilter(filterName = "LoginFilter",urlPatterns = {"/v1/user/article"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.debug("过滤器...");
        //先获取会话，先强制转换为HttpServletRequest和HttpServletResponse
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        UserVO user777888XXX =(UserVO) session.getAttribute("user777888XXX");
        if (user777888XXX != null){
            log.debug("用户已登录,放行...");
            chain.doFilter(request,response);
        }else {
            log.debug("用户未登录,重定向到登录页...");
            res.sendRedirect("/login.html");
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("过滤器初始化");
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        log.debug("过滤器销毁");
        Filter.super.destroy();
    }
}
